// 添加奖励

var projectType = {};

layui.use(['form', 'code', 'laydate','upload','layer','laytpl','element'], function() {
    var laydate = layui.laydate;
    form = layui.form;
    upload = layui.upload;
    layer = layui.layer;
    laytpl = layui.laytpl;
    element = layui.element;


    layui.code();

    // 时间选择
    laydate.render({
        elem: '.submitDate' //指定元素
    });
    // 时间选择
    laydate.render({
        elem: '.evaluationDate' //指定元素
    });


    $("#submitNewProject").click(function () {
        // 判断必填项--提示--统校验
        var params = {};

        // 集合所有参数
        $("#addNewProjectForm .save_val").each(function() {
            var name = $(this).attr("name");
            var val = $(this).val();
            params[name] = val;
        });

        if(!params.projectName){
            layer.msg("项目名称不能为空");
            return false;
        }
        if(!params.submitDate){
            layer.msg("上报日期不能为空");
            return false;
        }
        // if(!params.projectType){
        //     layer.msg("成果类型不能为空");
        //     return false;
        // }
        if(!params.evaluationDate){
            layer.msg("评估时间不能为空");
            return false;
        }
        if(!params.firstCharge){
            layer.msg("第一完成人不能为空");
            return false;
        }

        if(!params.context){
            layer.msg("成果概述不能为空");
            return false;
        }

        var url = cgiMain.academistParticipatedProjectAdd;
        if(params.id && params.id > 0){
            url = cgiMain.academistParticipatedProjectEdit;
        }


        AJAX.post(url,
            params,
            function (res) {
                if(res.code=='0000'){
                    layer.msg('保存成功');
                    $("#myModal").hide();
                    window.location.reload();
                }
            });
        return false;
    });



    function  setSelect(dom,data){
        for(let item in data){
            $(dom).append("<option value='" + item +"'>"+ data[item] +"</option>")
        }
        form.render("select");
    }



// 添加教育经验
    $(".add_btn").on("click", function() {
        $("#myModal").show();
        $("#myModal input, #myModal textarea").val("");
    });




    //初始化列表
    setTimeout(function () {
        AJAX.post(cgiMain.academistParticipatedProjectList,
            {},
            function (res) {

                if(res.code == '0000'){
                    $(".projectList").data("projectList",res.result);
                    var tpl = $("#projectItemTpl").html();
                    for(var i = 0; i < res.result.length;i++){
                        laytpl(tpl).render(res.result[i],function (html) {
                            $(".projectList").append(html);
                            $(".projectList").on("click",'.editProject',function (elem) {
                                editProject(elem);

                            });
                            $(".projectList").on("click",'.deleteProject',function (elem) {
                                deleteProject(elem);
                            });
                        });
                    }
                    element.render(".projectList");
                }

            });

    },500);


    function editProject(dom){

        var projectId = $(dom.target).attr("data-id");
        var projectList = $(".projectList").data("projectList");

        var academistProject={};
        for(var i = 0 ; i < projectList.length;i++){
            if(projectList[i].id == projectId){
                academistProject = projectList[i];
                break;
            }
        }

        $("input[name='id']").val(academistProject.id);
        $(".projectName").val(academistProject.projectName);
        $(".submitDate").val(academistProject.submitDate);
        $(".projectType").val(academistProject.projectType);
        $(".firstCharge").val(academistProject.firstCharge);
        $(".evaluationDate").val(academistProject.evaluationDate);
        $(".context").val(academistProject.context);

        $("#myModal").show();
    }

    function deleteProject(dom){

        var patentId = $(dom.target).attr("data-id");

        layer.open({
            content: '确定删除吗?'
            ,btn: ['删除', '取消']
            ,yes: function(index, layero){
                //按钮【按钮一】的回调
                AJAX.post(cgiMain.academistParticipatedProjectDelete,
                    {id:patentId},
                    function (res) {
                        if(res.code=='0000'){
                            layer.msg('删除成功');
                            window.location.reload();
                        }
                    });


            }
            ,btn2: function(index, layero){
                //按钮【按钮二】的回调
            }

            ,cancel: function(){
                //右上角关闭回调
            }
        });


    }



    $(".modal-footer > .layui-btn-primary").on("click",function () {
        $("#myModal").hide();
        $("#myModal input, #myModal textarea").val("");
    });

});
